﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace store.cyt369.com.MODELS
{
    public class Base64
    {
        private static string ToJsonString<T>(T obj)
        {
            return System.Text.Json.JsonSerializer.Serialize(obj);
        }

        public static string ObjectToBase64(object obj = null)
        {
            var ss = ToJsonString(obj);

            var str = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(ss));
            return System.Web.HttpUtility.UrlEncode(str);
        }

        public static Newtonsoft.Json.Linq.JObject Base64ToJSON(string base64)
        {
            if (!string.IsNullOrEmpty(base64))
            {
                try
                {
                    base64 = System.Web.HttpUtility.UrlDecode(base64);
                    var json = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(base64));
                    return Newtonsoft.Json.Linq.JObject.Parse(json);
                }
                catch { }
            }

            return null;
        }

    }
}
